<template>
  <div>
    <div style="margin: 10px 0;">
      <el-button type="primary" @click="dialogFormVisible=true">新增</el-button>
    </div>

  <el-table
    :data="auth.list"
    style="width: 100%">
    <el-table-column
      prop="title"
      label="名称"
      width="180">
    </el-table-column>
    <el-table-column
      prop="auth"
      label="权限">
    </el-table-column>
    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button
          size="mini"
          @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>

<!--    弹窗-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="authinfo" ref="authinfo" label-width="80px">
        <el-form-item label="名称" prop="title"><el-input v-model="authinfo.title" placeholder="输入名称" class="w300"></el-input></el-form-item>

        <el-form-item label="权限" prop="authid">
          <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
          <div style="margin: 15px 0;"></div>
          <el-checkbox-group v-model="authinfo.authid" @change="handleCheckedCitiesChange">
            <el-checkbox v-for="(model,id) in auth.model" :label="id" :key="id" v-model="id">{{model}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="editAuth">提交</el-button>
          <el-button type="info" @click="resetForm">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
<!--    弹窗-->

  </div>

</template>

<script>
    import {mapActions,mapState} from 'vuex'
    import {editAuth} from "@/apilist/Personnel";

    export default {
        name: "manageAuth",
        data(){
            return {
                dialogFormVisible: false,
                title: '',
                authinfo: {
                    title: '',
                    authid: [],
                    id: 0,
                },
                checkAll: false,
                isIndeterminate: true,
            }
        },
        computed:{
            ...mapState("Common",["auth"]),
        },
        methods:{
           ...mapActions("Common",["getAuthListActions"]),

            handleEdit(index,row){
                this.title = row.title
                this.authinfo.authid = row.authid
                this.authinfo.title = row.title
                this.authinfo.id = row.id
                this.dialogFormVisible = true
            },
            handleCheckAllChange(val) {
                let model_ids = [];
                let modelList = this.auth.model
                for(let key in modelList){
                   model_ids.push(key);
                }
                this.authinfo.authid = val ? model_ids : [];
                this.isIndeterminate = false;
            },
            handleCheckedCitiesChange(value) {
                let checkedCount = value.length;
                this.checkAll = checkedCount === this.auth.model.length;
                this.isIndeterminate = checkedCount > 0 && checkedCount < this.auth.model.length;
            },
            editAuth(){
                editAuth(this.authinfo).then(res => {
                   this.$message({
                       type: 'success',
                       message: res.msg,
                       onClose:()=>{
                           this.dialogFormVisible = false
                           this.getAuthListActions()
                       }
                   })
                })
            },
            resetForm(){
                this.authinfo.id = 0;
                this.authinfo.authid = [];
                this.authinfo.title = '';
                this.dialogFormVisible = false
            }
        },
    }
</script>

<style scoped>

</style>
